<?php

namespace App\Miya\Tools;

use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class ImageTool
{
    //图片大小优化
    static public function optimize($path)
    {
        if (!config('miya.open_image_optimize')) return;

        $source = public_path('uploads/' . $path);
        if (!file_exists($source)) return;

        $size = Storage::disk('admin')->size($path) / 1024 / 1024;
        if ($size < config('miya.image_size')) return;

        try {
            $resource = Image::make($source);
            $width = $resource->width();
            if ($width > config('miya.image_width')) {
                $resource->resize(config('miya.image_width'), null, function ($constraint) {
                    $constraint->aspectRatio();
                })->save($source);
            }
        } catch (\Exception $exception) {
            return;
        }
    }
}
